package gpl.pierrick.brihaye.aramorph.lucene;
import gpl.pierrick.brihaye.util.RangeSet;

import java.io.IOException;
import java.io.StringReader;
import java.util.Set;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;

/**
 * A filter that normalizes arabic words to some simpler format.
 * It removes all accents (tashkil) and kashida (tatweel).
 * These rules overcome the most spelling mistakes people do.
 * @author Ahmed Saad
 */
public class ArabicTashkilFilter extends ArabicNormalizer {

	private static final Set<Integer> tashkilSet = getNormalizer();
	
	public ArabicTashkilFilter(TokenStream input) {
		super(input);
	}

	private static Set<Integer> getNormalizer() {
		Set<Integer> tashkilSet = new RangeSet();
		tashkilSet.add((int)'\u0640'); //U+0640 : ARABIC TATWEEL
		tashkilSet.add((int)'\u064B'); //U+064B : ARABIC FATHATAN
		tashkilSet.add((int)'\u064C'); //U+064C : ARABIC DAMMATAN
		tashkilSet.add((int)'\u064D'); //U+064D : ARABIC KASRATAN
		tashkilSet.add((int)'\u064E'); //U+064E : ARABIC FATHA
		tashkilSet.add((int)'\u064F'); //U+064F : ARABIC DAMMA
		tashkilSet.add((int)'\u0650'); //U+0650 : ARABIC KASRA
		tashkilSet.add((int)'\u0651'); //U+0651 : ARABIC SHADDA
		tashkilSet.add((int)'\u0652'); //U+0652 : ARABIC SUKUN
		//Set of tashkil letters
		return tashkilSet;
	}
	
	/**
	 * This function normalizes an Arabic text for most common errors done by people.
	 * This helps indexing it in a normalized form so that they can be searched easily.
	 * @param word the word to be normalized.
	 */
	protected String clean(String word) {
		return removeTashkil(word);
	}
	
	public static String removeTashkil(String word) {
		char[] cleanWord = new char[word.length()];
		int i=0;
		char[] arGloss = word.toCharArray();
		
		for (int x = 0;x < arGloss.length;x++) {
			//copy it iff it is not tashkil
			if (!tashkilSet.contains(new Integer(arGloss[x])))
				cleanWord[i++] = arGloss[x];
		}
		return i!=0?new String(cleanWord,0,i):null;
	}

	public static void main(String[] args) {
		try {
			String arabicText = "أًنا إٍسمي أَحمد";
			ArabicTokenizer at = new ArabicTokenizer(new StringReader(arabicText));
			ArabicTashkilFilter an = new ArabicTashkilFilter(at);
			while (an.incrementToken()) {
//				System.out.print(an.getAttribute(PositionIncrementAttribute.class).getPositionIncrement());
//				System.out.print(':');
//				System.out.println(an.getAttribute(TermAttribute.class).term());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
